import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:ost_flutter_module/class/entity/store/shop_cart_entity.dart';
import 'package:ost_flutter_module/class/store/course_page/widgets/store_course_home_card.dart';
import 'package:ost_flutter_module/class/store/shop_cart/store_shop_cart_bottom_navigaton_bar.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';
import 'package:ost_flutter_module/common/empty_page.dart';
import 'package:ost_flutter_module/common/styles.dart';
import 'package:ost_flutter_module/common/utils.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_radius_container.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_scaffold.dart';
import 'package:ost_flutter_module/widget/custom_widget/custome_size_icon_button.dart';
import 'package:ost_flutter_module/widget/loading/loading_empty_widget.dart';
import 'package:ost_flutter_module/widget/loading/loading_error_widget.dart';
import 'package:ost_flutter_module/widget/loading/loading_view.dart';
import 'package:ost_flutter_module/widget/loading/loading_widget.dart';

import 'bloc/shop_cart_bloc.dart';

class StoreShopCartPageBody extends StatefulWidget {
  StoreShopCartPageBody({Key key}) : super(key: key);

  @override
  _StoreShopCartPageBodyState createState() => _StoreShopCartPageBodyState();
}

class _StoreShopCartPageBodyState extends State<StoreShopCartPageBody> {
  @override
  void initState() {
    super.initState();
  }

  void onDelete(BuildContext context, int index) async {
    var result = await showOkCancelAlertDialog(
        context: context, title: '提示', message: '确定要从购物车移除该商品吗？');

    if (result == OkCancelResult.ok) {
      // _list.removeAt(index);
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<ShopCartBloc, ShopCartState>(
      listener: (context, state) {
        // TODO: implement listener
      },
      child: BlocBuilder<ShopCartBloc, ShopCartState>(
        builder: (context, state) {
          return GScaffold(
            title: Text('购物车', style: TextStyles.appBarTitleStyle),
            body: _buildBody(context),
            bottomNavigationBar: StoreShopCartBottomNavigationBar(onTap: () {}),
          );
        },
      ),
    );
  }

  _buildBody(BuildContext context) {
    var _dataModel = context.read<ShopCartBloc>().dataModel;
    var _datas = _dataModel.dataList;

    return LoadingView(
      status: _dataModel?.loadingStatus,
      loadingContent: LoadingWidget(),
      errorContent: LoadingErrorWidget(
        errorTitle: _dataModel?.errorMsg,
        onRetry: () => context.read<ShopCartBloc>().add(
              ShopCartFetchData(
                  // isRefresh: true,
                  action: null),
            ),
      ),
      successContent: ListView.builder(
        itemCount: _datas.length,
        itemBuilder: (BuildContext context, int index) {
          ShopCartEntity entity = _datas[index];
          return _buildSlidableCard(context, index: index, entity: entity);
        },
      ),
      emptyContent: LoadingEmptyWidget(text: '购物车还是空的~'),
    );
  }

  _buildSlidableCard(BuildContext context, {int index, ShopCartEntity entity}) {
    return Padding(
      padding: EdgeInsets.only(
          top: Adapt.px(Utils.padding), right: Adapt.px(Utils.padding)),
      child: Slidable(
        key: Key(entity.cartId),
        child: SlideActionCard(
          row: index,
          check: entity.check,
          entity: entity,
          onPressed: (value) {
            entity.check = value;
            setState(() {});
          },
        ),
        actionPane: SlidableBehindActionPane(),
        actionExtentRatio: 0.25,
        secondaryActions: [
          ClipRRect(
            borderRadius: BorderRadius.only(
                topRight: Radius.circular(Adapt.px(Utils.radius)),
                bottomRight: Radius.circular(Adapt.px(Utils.radius))),
            child: SlideAction(
              child: Text('删除', style: TextStyles.delTitleStyle),
              color: colorFF5454,
              onTap: () => onDelete(context, index),
            ),
          ),
        ],
        dismissal: SlidableDismissal(
          child: SlidableDrawerDismissal(),
          onWillDismiss: (_) async {
            var result = await showOkCancelAlertDialog(
                context: context, title: '提示', message: '确定要从购物车移除该商品吗？');

            if (result == OkCancelResult.ok) {
              // _list.removeAt(index);
              setState(() {});
              return true;
            }
            return false;
          },
          onDismissed: (actionType) {},
        ),
      ),
    );
  }
}

class SlideActionCard extends StatelessWidget {
  final int row;
  final bool check;
  final ShopCartEntity entity;
  final ValueChanged<bool> onPressed;

  SlideActionCard(
      {Key key, this.check = false, this.onPressed, this.row, this.entity})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        GIconButton(
          svg: check
              ? Utils.svgAssets('icon_selected')
              : Utils.svgAssets('icon_unselected'),
          onPressed: () => onPressed(!check),
        ),
        Expanded(
          child: GContainer(
            padding: EdgeInsets.zero,
            child: CourseCardWidget(),
          ),
        ),
      ],
    );
  }
}
